package com.goods.controller.business;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.service.SupplierService;
import com.goods.common.model.business.Supplier;
import com.goods.common.response.ResponseBean;
import com.goods.common.vo.business.SupplierVO;
import com.goods.common.vo.system.PageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 物资来源
 */
@Api(tags = "业务管理-物资来源接口")
@RestController
@RequestMapping("/business/supplier")
public class SupplierController {
    @Autowired
    private SupplierService supplierService;

    //加载物资来源列表
    @ApiOperation(value = "加载物资来源列表")
    @GetMapping("/findSupplierList")
    public ResponseBean<PageVO<SupplierVO>> findSupplierList(
            @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize") Integer pageSize,
            SupplierVO supplierVO
    ){
        //获取物资来源集合
        PageHelper.startPage(pageNum,pageSize);
        List<Supplier> list = supplierService.findSupplierList(pageNum,pageSize,supplierVO);
        PageInfo<Supplier> pageInfo = new PageInfo<>(list);
        List<SupplierVO> listVO = list.stream().map(supplier -> {
            SupplierVO s1 = new SupplierVO();
            BeanUtils.copyProperties(supplier, s1);
            return s1;
        }).collect(Collectors.toList());

        PageVO<SupplierVO> pageVO = new PageVO<SupplierVO>(pageInfo.getTotal(),listVO);
        return ResponseBean.success(pageVO);
    }

    //打开编辑和表单的回显
    @ApiOperation(value = "打开编辑和表单的回显")
    @GetMapping("/edit/{id}")
    public ResponseBean edit(@PathVariable Long id){
        SupplierVO supplierVO = supplierService.edit(id);
        return ResponseBean.success(supplierVO);
    }

    //修改页面的保存
    @ApiOperation(value = "修改页面的保存")
    @PutMapping("/update/{id}")
    public ResponseBean update(@PathVariable Long id, @RequestBody SupplierVO supplierVO){
        supplierService.update(supplierVO);
        return ResponseBean.success();
    }

    //添加物资来源
    @ApiOperation(value = "添加物资来源")
    @PostMapping("/add")
    public ResponseBean add(@RequestBody SupplierVO supplierVO){
        supplierService.add(supplierVO);
        return ResponseBean.success();
    }

    //删除物资来源
    @ApiOperation(value = "添加物资来源")
    @DeleteMapping("/delete/{id}")
    public ResponseBean delete(@PathVariable Long id){
        supplierService.delete(id);
        return ResponseBean.success();
    }

    //加载来源数据
    @GetMapping("/findAll")
    public ResponseBean findAll(){
        List<SupplierVO> list = supplierService.findAll();
        return ResponseBean.success(list);
    }
}
